def LOWORD(lParam):
    return lParam & 0xffff

def HIWORD(lParam):
    return lParam >> 16 & 0xffff

class MessageHandler:
    def __init__(self, msg=None):
        if isinstance(msg, MessageHandler):
            self.msg = msg.msg.copy()
        else:
            self.msg = {}

    def MESSAGE_HANDLER(self, msg):
        '''def OnMessageHandler(self, uMsg, wParam, lParam)'''
        def _func1(func):
            def _func2(cls, message, hWnd, wParam, lParam):
                return bool(func(cls, message, wParam, lParam))
            self.msg[msg] = _func2
            return _func2
        return _func1

    def MSG_WM_CREATE(self, func):
        '''OnCreate(self, lpCreateStruct)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, lParam))
        self.msg[0x0001] = _func1
        return _func1

    def MSG_WM_INITDIALOG(self, func):
        '''OnInitDialog(self, hWnd, lParam)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, hWnd, lParam))
        self.msg[0x0110] = _func1
        return _func1

    def MSG_WM_DESTROY(self, func):
        '''OnDestroy(self)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls))
        self.msg[0x0002] = _func1
        return _func1

    def MSG_WM_MOVE(self, func):
        '''OnMove(self, x, y)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, LOWORD(lParam), HIWORD(lParam)))
        self.msg[0x0003] = _func1
        return _func1

    def MSG_WM_SIZE(self, func):
        '''OnSize(self, nType, width, height)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam, LOWORD(lParam), HIWORD(lParam)))
        self.msg[0x0005] = _func1
        return _func1

    def MSG_WM_ACTIVATE(self, func):
        '''OnActivate(self, nState, bMinimized, wndOther)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, LOWORD(wParam), bool(HIWORD(wParam)), lParam))
        self.msg[0x0006] = _func1
        return _func1

    def MSG_WM_SETFOCUS(self, func):
        '''OnSetFocus(self, wndOld)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam))
        self.msg[0x0007] = _func1
        return _func1

    def MSG_WM_KILLFOCUS(self, func):
        '''OnKillFocus(self, wndFocus)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam))
        self.msg[0x0008] = _func1
        return _func1

    def MSG_WM_PAINT(self, func):
        '''OnPaint(self, dc)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam))
        self.msg[0x000F] = _func1
        return _func1

    def MSG_WM_CLOSE(self, func):
        '''OnClose(self)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls))
        self.msg[0x0010] = _func1
        return _func1

    def MSG_WM_SYSCOLORCHANGE(self, func):
        '''OnSysColorChange(self)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls))
        self.msg[0x0015] = _func1
        return _func1

    def MSG_WM_SHOWWINDOW(self, func):
        '''OnShowWindow(self, bShow, nStatus)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, bool(wParam), lParam))
        self.msg[0x0018] = _func1
        return _func1

    def MSG_WM_CANCELMODE(self, func):
        '''OnCancelMode(self)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls))
        self.msg[0x001F] = _func1
        return _func1

    def MSG_WM_MOUSEACTIVATE(self, func):
        '''OnMouseActivate(self, wndTopLevel, nHitTest, message)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam, LOWORD(lParam), HIWORD(lParam)))
        self.msg[0x0021] = _func1
        return _func1

    def MSG_WM_CHILDACTIVATE(self, func):
        '''OnChildActivate(self)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls))
        self.msg[0x0022] = _func1
        return _func1

    def MSG_WM_KEYDOWN(self, func):
        '''OnKeyDown(self, nChar, nRepCnt, nFlags)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam, lParam & 0xFFFF, (lParam & 0xFFFF0000) >> 16))
        self.msg[0x0100] = _func1
        return _func1

    def MSG_WM_KEYUP(self, func):
        '''OnKeyUp(self, nChar, nRepCnt, nFlags)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam, lParam & 0xFFFF, (lParam & 0xFFFF0000) >> 16))
        self.msg[0x0101] = _func1
        return _func1

    def MSG_WM_CHAR(self, func):
        '''OnChar(self, nChar, nRepCnt, nFlags)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam, lParam & 0xFFFF, (lParam & 0xFFFF0000) >> 16))
        self.msg[0x0102] = _func1
        return _func1

    def MSG_WM_DEADCHAR(self, func):
        '''OnDeadChar(self, nChar, nRepCnt, nFlags)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam, lParam & 0xFFFF, (lParam & 0xFFFF0000) >> 16))
        self.msg[0x0103] = _func1
        return _func1

    def MSG_WM_SYSKEYDOWN(self, func):
        '''OnSysKeyDown(self, nChar, nRepCnt, nFlags)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam, lParam & 0xFFFF, (lParam & 0xFFFF0000) >> 16))
        self.msg[0x0104] = _func1
        return _func1

    def MSG_WM_SYSKEYUP(self, func):
        '''OnSysKeyUp(self, nChar, nRepCnt, nFlags)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam, lParam & 0xFFFF, (lParam & 0xFFFF0000) >> 16))
        self.msg[0x0105] = _func1
        return _func1

    def MSG_WM_SYSCHAR(self, func):
        '''OnSysChar(self, nChar, nRepCnt, nFlags)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam, lParam & 0xFFFF, (lParam & 0xFFFF0000) >> 16))
        self.msg[0x0106] = _func1
        return _func1

    def MSG_WM_SYSDEADCHAR(self, func):
        '''OnSysDeadChar(self, nChar, nRepCnt, nFlags)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam, lParam & 0xFFFF, (lParam & 0xFFFF0000) >> 16))
        self.msg[0x0107] = _func1
        return _func1

    def MSG_WM_TIMER(self, func):
        '''OnTimer(self, nIDEvent)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam))
        self.msg[0x0113] = _func1
        return _func1

    def MSG_WM_HSCROLL(self, func):
        '''OnHScroll(self, nSBCode, nPos, pScrollBar)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, LOWORD(wParam), HIWORD(wParam), lParam))
        self.msg[0x0114] = _func1
        return _func1

    def MSG_WM_VSCROLL(self, func):
        '''OnVScroll(self, nSBCode, nPos, pScrollBar)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, LOWORD(wParam), HIWORD(wParam), lParam))
        self.msg[0x0115] = _func1
        return _func1

    def MSG_WM_MOUSEMOVE(self, func):
        '''OnMouseMove(self, nFlags, x, y)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam, LOWORD(lParam), HIWORD(lParam)))
        self.msg[0x0200] = _func1
        return _func1

    def MSG_WM_MOUSEWHEEL(self, func):
        '''OnMouseWheel(self, nFlags, zDelta, x, y)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, LOWORD(wParam), HIWORD(wParam), LOWORD(lParam), HIWORD(lParam)))
        self.msg[0x020A] = _func1
        return _func1

    def MSG_WM_LBUTTONDOWN(self, func):
        '''OnLButtonDown(self, nFlags, x, y)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam, LOWORD(lParam), HIWORD(lParam)))
        self.msg[0x0201] = _func1
        return _func1

    def MSG_WM_LBUTTONUP(self, func):
        '''OnLButtonUp(self, nFlags, x, y)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam, LOWORD(lParam), HIWORD(lParam)))
        self.msg[0x0202] = _func1
        return _func1

    def MSG_WM_LBUTTONDBLCLK(self, func):
        '''OnLButtonDblClk(self, nFlags, x, y)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam, LOWORD(lParam), HIWORD(lParam)))
        self.msg[0x0203] = _func1
        return _func1

    def MSG_WM_RBUTTONDOWN(self, func):
        '''OnRButtonDown(self, nFlags, x, y)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam, LOWORD(lParam), HIWORD(lParam)))
        self.msg[0x0204] = _func1
        return _func1

    def MSG_WM_RBUTTONUP(self, func):
        '''OnRButtonUp(self, nFlags, x, y)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam, LOWORD(lParam), HIWORD(lParam)))
        self.msg[0x0205] = _func1
        return _func1

    def MSG_WM_RBUTTONDBLCLK(self, func):
        '''OnRButtonDblClk(self, nFlags, x, y)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam, LOWORD(lParam), HIWORD(lParam)))
        self.msg[0x0206] = _func1
        return _func1

    def MSG_WM_MBUTTONDOWN(self, func):
        '''OnMButtonDown(self, nFlags, x, y)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam, LOWORD(lParam), HIWORD(lParam)))
        self.msg[0x0207] = _func1
        return _func1

    def MSG_WM_MBUTTONUP(self, func):
        '''OnMButtonUp(self, nFlags, x, y)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam, LOWORD(lParam), HIWORD(lParam)))
        self.msg[0x0208] = _func1
        return _func1

    def MSG_WM_MBUTTONDBLCLK(self, func):
        '''OnMButtonDblClk(self, nFlags, x, y)'''
        def _func1(cls, message, hWnd, wParam, lParam):
            return bool(func(cls, wParam, LOWORD(lParam), HIWORD(lParam)))
        self.msg[0x0209] = _func1
        return _func1
